Skip to content

Facade: OpenRowReader(Async) and CreateEncoder(Stream) streaming entry points - #12

Merged
sdcb merged 1 commit into
mainfrom
streaming/facade
Nov 23, 2026
Merged

sdcb merged 1 commit into
mainfrom
streaming/facade

Conversation

@sdcb

@sdcb sdcb commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Unit 1 of the streaming batch: the facade's stream entry points, on top of the Abstractions foundation (84b58f2). No codec package or Abstractions change; ImageDecoder.cs untouched.

What changes

  • ImageCodecs.OpenRowReader(stream, format, options, leaveOpen, ct) / OpenRowReaderAsync(...) return an IImageRowReader. The stream is wrapped in a StreamInput, and the sniff reads 64 bytes, or a leading ISO-BMFF ftyp box whole (up to 4 KiB). The sniff always sees the same bytes whatever the read sizes, so the codec chosen never depends on the feed. Dispatch uses the registry order of the span Find. A codec with CreateRowReader gets its streaming reader; any other codec gets a BufferedRowReader. Its failures carry the codec's reason as InvalidDataException (via DecodeFailure), and an image with no integer projection (float, PQ/HLG) throws NotSupportedException, as Decode(data, format) does. An unknown format throws InvalidDataException(NoCodecMessage), matching ImageDecoder.Open. The header is read before the method returns. On failure the stream is disposed unless leaveOpen is set.
  • ImageCodecs.CreateEncoder(format, Stream, options, leaveOpen) returns the new public StreamEncoderSession (IImageEncoderSession, IAsyncDisposable) with StreamFrameEncoder frames. The format's session writes into an internal pooled chunk writer (StreamBufferWriter, 64 KiB). WriteRows / Commit write synchronously, flushing a chunk as soon as it fills. WriteRowsAsync / CommitAsync only write asynchronously: they encode synchronously, then await the write of the pending bytes. After a large write the buffer shrinks back to one chunk. IsStreaming forwards the inner session's. Commit flushes the stream.
  • The Encode(..., Stream) overloads write through the same chunk writer, so the bytes are the same as before and there is no whole-file ArrayBufferWriter. The docs no longer say "buffered, then written in one call" or suggest PipeWriter, and now note that a failed encode may leave a partial file.
  • NoProjection was factored out of Decode(data, format) without changing its messages. New internal test hooks: EncoderSnapshot / ExchangeEncoders.
  • The AotSmoke FacadeSmoke (shared with WasmSmoke) gains two checks. A sync OpenRowReader over a non-seekable stream runs for every sample, and the rows must equal TryDecodeInto. For every built-in encoder, CreateEncoder(Stream) with WriteRowsAsync/CommitAsync must produce the bytes of Encode, and OpenRowReaderAsync must return the same rows as TryDecodeInto.
  • README.md and README.zh-CN.md: a short "Streaming" subsection.

Tests (tests/.../Streaming/FacadeStreamingTests.cs, in the registry collection)

  • RawStreamCodec registered first: every feed, including async-only, equals TryDecodeInto with IsStreaming true; the codec's fallback variant gives IsStreaming false; the reader reads only as far as the rows need.
  • NonStreamingRegistration goes through BufferedRowReader with equal rows. Every built-in format, plus WebP lossy/lossless, JBIG2 and JP2 fixtures, at full size, 1/2 scale and a region.
  • Unknown, empty and HEIF-unregistered input fail with the expected messages; a long ftyp box with avif past byte 64 gives the AVIF message. Truncated or corrupt PNG throws InvalidDataException under every feed, with the format-prefixed reason. Float PFM throws NotSupportedException. leaveOpen is honoured on success and on failure, and cancellation is checked.
  • CreateEncoder(Stream) bytes equal CreateEncoder(IBufferWriter) for every encoder format, both sync in bands and async-only into a stream whose sync Write/Flush throw. A fake streaming encoder shows bytes reaching the stream before Commit, never more than about one chunk held back; I broke the async flush on purpose and this test then failed. Argument and disposed-state checks.
  • Encode(Stream) bytes are unchanged and the stream is not flushed.
  • End-to-end: JPEG, PNG and WebP sources go OpenRowReaderAsync (async-only ChunkedStream) → CreateEncoder(Png, async-only stream) → WriteRowsAsync/CommitAsync; the re-decoded pixels equal TryDecodeInto of the source.

Validation (Windows, this worktree)

  • dotnet build SimdCodecs.slnx -c Release: 0 warnings.
  • Full suite: 7052 tests, 0 failures (393 skipped: oracle tools not installed here).
  • The streaming test classes pass under DOTNET_EnableAVX2=0 and DOTNET_EnableHWIntrinsic=0.
  • AotSmoke/run.sh: NativeAOT output identical to the JIT's (402 lines, default and native ISA). WasmSmoke/run.sh: Mono AOT output identical (402 lines).

No soak command is needed: no entropy decoder or container parser changed.

Notes for other units: a gain-map JPEG sniffs as plain JPEG on the stream path, because the gain map lies past the sniffed bytes. Its rows are the SDR base, which is also what TryDecodeInto gives. Each smoke line records whether every encoder session streams (s/b), so that line changes when a codec gains a streaming session, identically under JIT and AOT.

🤖 Generated with Claude Code

- ImageCodecs.OpenRowReader / OpenRowReaderAsync sniff a forward-only stream (64 bytes, or a
  leading ftyp box whole up to 4 KiB, so the choice never depends on how reads arrive), then use
  the codec's CreateRowReader or a BufferedRowReader whose failures carry the codec's reason
  (InvalidDataException) or NotSupportedException for an image with no integer projection.
  Unknown formats throw InvalidDataException(NoCodecMessage), as ImageDecoder.Open does.
- ImageCodecs.CreateEncoder(format, Stream, options, leaveOpen) returns StreamEncoderSession /
  StreamFrameEncoder: the format's session over a pooled 64 KiB chunk writer; WriteRows/Commit
  write synchronously, WriteRowsAsync/CommitAsync only asynchronously. IsStreaming forwards.
- Encode(..., Stream) overloads write through the same chunk writer (same bytes, no whole-file
  ArrayBufferWriter); docs updated.
- Test hooks EncoderSnapshot / ExchangeEncoders; FacadeStreamingTests; AotSmoke/WasmSmoke cover
  the new entry points; README (en + zh-CN) Streaming subsection.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sdcb
sdcb merged commit 368468a into main Nov 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant